<template>
  <div class="login_container">
    <div class="login_box">
<!--      头像-->
      <div class="avatar_box">
        <img src="../assets/img/bg1.jpg" alt="">
      </div>
<!--      登录-->
      <el-form ref="loginformref" :rules="loginFormRules" :model="loginForm" label-width="0px" class="login_form">
        <!--          账号-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="请输入账号"></el-input>
        </el-form-item>
        <!--          密码-->
        <el-form-item prop="password">
          <el-input class="pass_input" :type="pwdtype" placeholder="请输入密码"
                    v-model="loginForm.password" prefix-icon="el-icon-lock"></el-input>
          <img :src="openeye" class="show_hid" @click="changetype">
        </el-form-item>
<!--        按钮区域-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetloginform">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      },
      pwdtype: 'password',
      openeye: require('../assets/img/eye.png')
    }
  },
  methods: {
    // 重置登录表单
    resetloginform () {
      this.$refs.loginformref.resetFields()
      this.$message.success('重置成功')
    },
    changetype () {
      this.pwdtype = this.pwdtype === 'password' ? 'text' : 'password'
      this.openeye = this.openeye === require('../assets/img/eye1.png') ? require('../assets/img/eye.png') : require('../assets/img/eye1.png')
    },
    login () {
      this.$refs.loginformref.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功')
        // 将登录成功后的token保存到客户端的sessionstorage中
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped>
  .login_container {
    background: url("../assets/img/bg.png");
    background-size: 250px 250px;
    height: 100%;
  }
  .login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
  }
  .avatar_box{
    position: absolute;
    top: -60%;
    left: 50%;
    transform: translate(-50%,50%);
    border-radius: 50%;
    overflow: hidden;
    border: 1px solid #b2b2b2;
    background-color: #F2F2F2;
    box-shadow: 1px 1px 5px #b2b2b2;
    padding: 10px;
    width: 150px;
    height: 150px;
  }
  .avatar_box img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eeeeee;
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
  .login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .show_hid{
    cursor: pointer;
    position: absolute;
    width: 20px;
    height: 20px;
    top: 12px;
    right: 10px;
  }
</style>
